package com.be.beadmin.quality.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.quality.dto.input.InspectionItemValueDto;
import com.be.beadmin.quality.dto.output.InspectionItemValueEx;
import com.be.beadmin.quality.service.InspectionItemValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zjm
* @date 2023-05-28
**/
@RestController
public class InspectionItemValueController {

    @Autowired
    private  InspectionItemValueService inspectionItemValueService;


    @PostMapping(value = "/quality/getInspectionItemValueByPage")
    public ResultModel getInspectionItemValueByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(inspectionItemValueService.getInspectionItemValueByPage(pageEntity)) ;
    }

    @PostMapping("/quality/addInspectionItemValue")
    public ResultModel addInspectionItemValue(@RequestBody InspectionItemValueDto inspectionItemValueDto){
        inspectionItemValueService.addInspectionItemValue(inspectionItemValueDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/quality/delInspectionItemValue")
    public ResultModel delInspectionItemValue(@RequestBody InspectionItemValueDto inspectionItemValueDto){
        inspectionItemValueService.delInspectionItemValue(inspectionItemValueDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/quality/updateInspectionItemValue")
    public ResultModel updateInspectionItemValue(@RequestBody InspectionItemValueDto inspectionItemValueDto){
        inspectionItemValueService.updateInspectionItemValue(inspectionItemValueDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/quality/exportInspectionItemValue")
    public void exportInspectionItemValue(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<InspectionItemValueEx> inspectionItemValuePage = inspectionItemValueService.getInspectionItemValueByPage(pageEntity);
        List<InspectionItemValueEx> inspectionItemValueExes = inspectionItemValuePage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("InspectionItemValue接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), InspectionItemValueEx.class).sheet("sheet1").doWrite(inspectionItemValueExes);
    }
}